package cn.zeal4j;

import java.util.Random;

/**
 * @author Administrator
 * @file Java-StandardEdition-Review
 * @create 2020 09 30 11:13
 */
public class FlowControl {

    public static void main(String[] args) {

        // 单IF使用
        if (true) {
            System.out.println("this code block will execute ...");
        }
        // IF + ELSE
        if (false) {
            System.out.println("the expression is false, this code block will not execute ...");
        } else {
            System.out.println("but else code block will execute ...");
        }

        // 多路判断
        int a1 = new Random().nextInt();
        int a2 = new Random().nextInt();
        if (a1 > a2) {
            System.out.println("a1 > a2 is true");
        } else if (a1 + 1000 > a2) {
            System.out.println("a1 + 1000 > a2 is true");
        } else if (a1 + 10000 > a2) {
            System.out.println("a1 + 10000 > a2 is true");
        } else {
            System.out.println("a1 is to short ...");
        }

        // 多IF情况，每个IF都会判断
        if (true) {
            System.out.println("first if is true");
        }
        if (true) {
            System.out.println("second if is true");
        }
        if (true) {
            System.out.println("third if is true");
        }

        // 使用switch
        int roll = new Random().nextInt(10) + 1;
        switch (roll) {
            default:
                System.out.println("默认情况，也就是除了case之外的情况执行的代码");
                break; // 注意确定的case就要用Break结束switch，否则switch继续向下执行
            case 2:
                System.out.println("the roll is 2 been case checked");
                break;
            case 4:
                System.out.println("the roll is 3 been case checked");
                break;
            case 7:
                System.out.println("the roll is 7 been case checked");
                // break; // 最后的可以不加，switch走到结尾就结束了
        }

        // 一些用法
        int month = new Random().nextInt(12) + 1;
        switch (month) {
            case 1:
            case 2:
            case 3:
                System.out.println("the season is spring");
                break;
            case 4:
            case 5:
            case 6:
                System.out.println("the season is summer");
                break;
            case 7:
            case 8:
            case 9:
                System.out.println("the season is autumn");
                break;
            case 10:
            case 11:
            case 12:
                System.out.println("the season is winter");
                break;
        }

        // 断言处理
        boolean i_am_java_noob = true;
        boolean is_it_sure = new Random().nextBoolean();
        assert (is_it_sure && i_am_java_noob);

        // 循环
        int loopCount = 1;

        boolean i_want_break = false;
        while (loopCount < 100) {
            System.out.println("the loop is executing ...");

            loopCount ++; // 停止循环的方式一，达到表达式的条件为false

            // 如果 想在循环中忽略什么，使用continue
            if (loopCount % 2 == 0) continue;

            if (loopCount == 30) i_want_break = true;
            if (i_want_break) break; // 方式二 ，直接break;
        }

        // 可以使用多嵌套循环 ,
        loopCount = 1;
        int innerLoopCount = 1;

        while (loopCount < 1000) {

            innerLoopCount = 1;
            while (innerLoopCount < 80 ) {

                innerLoopCount ++ ;
            }
            loopCount ++;
        }

        // do while 即只少执行一次的循环
        do {
            System.out.println("at least let me execute once ! ");
        } while (new Random().nextInt(100) == 78);

        // for 循环 ->  起始值 + 迭代条件 + 步长
        for (int i = 0; i < 100; i++) {
            System.out.println("count var i to increment, so this loop can be limited ... ");
        }

        // 标签设置
        for1:
        for (int i = 0; i < 10; i++) {

            for2:
            for (int j = 0; j < 20; j++) {

                for3:
                for (int k = 0; k < 30; k++) {

                    for4:
                    for (int l = 0; l < 40; l++) {

                        if (i == 3) break for4;
                        if (j == 4) continue for3;
                        if (k == 5) break for2;
                        if (l == 6) break for1;
                    }
                }
            }
        }
    }
}
